package com.voole.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.voole.entity.SysAction;
import com.voole.entity.SysAuthority;
import com.voole.entity.SysCatalog;
import com.voole.service.IMenuService;
import com.voole.service.ISysActionService;
import com.voole.service.ISysAuthorityService;
import com.voole.service.ISysCatalogService;
import com.voole.service.ISysResourceService;

@Controller
public class MenuController extends BaseController {

	private static Logger logger = Logger.getLogger(MenuController.class);
	
	@Autowired
	private IMenuService menuService;
	@Autowired
	private ISysAuthorityService sysAuthorityService;
	@Autowired
	private ISysCatalogService sysCatalogService;
	@Autowired
	private ISysResourceService sysResourceService;
	@Autowired
	private ISysActionService sysActionService;
	
	/**
	 * 配置管理 - 目录菜单管理 - 目录菜单
	 */
	@RequestMapping("/menu/sysmenu.do")
    public ModelAndView sysmenu(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView("sys_menu");
    }
	
	/**
	 * 获取目录菜单结构（顶侧，左侧主，左侧次）
	 */
	@RequestMapping("/menu/getMenuList.do")
    public ModelAndView getMenuList(HttpServletRequest request,HttpServletResponse response) {
		try {
			List<SysAuthority> menuList = menuService.getMenuList();
			this.ajaxComplete(menuList, response);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }
	
	/**
	 * 获取一级目录
	 */
	@RequestMapping("/menu/getLv1MenuList.do")
    public ModelAndView getLv1MenuList(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<SysAuthority> sysAuthorityList = sysAuthorityService.getList();
			this.ajaxComplete(sysAuthorityList, response);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }
	
	/**
	 * 获取二级目录
	 */
	@RequestMapping("/menu/getLv2MenuList.do")
    public ModelAndView getLv2MenuList(HttpServletRequest request, Integer authorityId, HttpServletResponse response) {
		try {
			List<SysCatalog> sysCatalogList = sysCatalogService.getListByAuthorityId(authorityId);
			this.ajaxComplete(sysCatalogList, response);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }
	
	/**
	 * 获取四级目录(页面功能)
	 */
	@RequestMapping("/menu/getLv4MenuList.do")
    public ModelAndView getLv4MenuList(HttpServletRequest request, Integer resourceid, HttpServletResponse response) {
		try {
			List<SysAction> actionList = sysActionService.getListByResourceId(resourceid);
			
			Map<String, Object> ajaxMap = new HashMap<String, Object>();
			ajaxMap.put("data", actionList);
			
			this.ajaxComplete(JSONObject.fromObject(ajaxMap).toString(), response);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }

	/**
	 * 获取 单个 目录菜单 信息
	 */
	@RequestMapping("/menu/getMenuInfo.do")
    public ModelAndView getMenuInfo(HttpServletRequest request,HttpServletResponse response) {
		try {
			String menuLv = request.getParameter("menuLv");
			String menuId = request.getParameter("menuId");
			this.ajaxComplete(menuService.getMenuInfo(menuLv, menuId), response);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }
	
	/**
	 * 保存目录菜单
	 */
	@RequestMapping("/menu/saveMenu.do")
    public ModelAndView saveMenu(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String level = request.getParameter("level");
			String menuLv1Id = request.getParameter("menulv1");
			String menuLv2Id = request.getParameter("menulv2");
			String menuLv3Id = request.getParameter("menulv3");
			String pageURL = request.getParameter("pageurl");
			String menuName = request.getParameter("menuname");
			String sort = request.getParameter("sort");
			String isbutton = request.getParameter("isbutton");
			String code = request.getParameter("code");
			String note = request.getParameter("note");
			
			if("1".equals(level)){
				sysAuthorityService.addAuthority(menuName, Integer.parseInt(sort), 0);
			}else if("2".equals(level)){
				sysCatalogService.addCatalog(menuName, Integer.parseInt(menuLv1Id), Integer.parseInt(sort), 0);
			}else if("3".equals(level)){
				sysResourceService.addResource(menuName, Integer.parseInt(menuLv2Id), pageURL, Integer.parseInt(sort), 0);
			}else if("4".equals(level)){
				sysActionService.addAction(menuName, Integer.parseInt(menuLv3Id), pageURL, Integer.parseInt(isbutton), code, note, Integer.parseInt(sort), 0);
			}
			result.put("msg", "操作成功");
			
		} catch (Exception e) {
			logger.error(e);
			result.put("msg", e.getMessage());
		}
		this.ajaxComplete(result, response);
		return null;
    }
	
	/**
	 * 删除目录菜单
	 */
	@RequestMapping("/menu/delMenu.do")
    public ModelAndView delMenu(HttpServletRequest request,HttpServletResponse response) {
		try {
			String menuLv = request.getParameter("editMenuLv");
			String menuId = request.getParameter("editMenuId");
			this.ajaxComplete(menuService.delMenu(menuLv, menuId), response);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }
	
	/**
	 * 修改目录菜单
	 */
	@RequestMapping("/menu/updateMenu.do")
    public ModelAndView updateMenu(HttpServletRequest request,HttpServletResponse response) {
		try {
			String menuLv = request.getParameter("editMenuLv");
			String menuId = request.getParameter("editMenuId");
			String menuUrl = request.getParameter("editMenuUrl");
			String menuName = request.getParameter("editMenuName");
			String menuSort = request.getParameter("editMenuSort");
			String isbutton = request.getParameter("isbutton");
			String actioncode = request.getParameter("actioncode");
			String actionnote = request.getParameter("actionnote");
			this.ajaxComplete(menuService.updateMenu(menuLv, menuId, menuUrl, menuName, menuSort, isbutton, actioncode, actionnote), response);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
    }
	
}
